﻿using System;
using System.ComponentModel;

namespace Sanjeet.FastCopy.FileCopyLogic.Design3
{
    internal interface IAsyncWorker
    {
        bool CacellationPending { get; }
        bool IsBusy { get; }
        void Start();
        void Stop();
        event EventHandler<EventArgs> StatusChanged;
    }

    /// <summary>
    /// 
    /// </summary>
    internal abstract class AsyncWorkerBase : IAsyncWorker
    {
        protected readonly BackgroundWorker worker;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="worker"></param>
        public AsyncWorkerBase(BackgroundWorker worker)
        {
            this.worker = worker;
            this.worker.WorkerReportsProgress = true;
            this.worker.WorkerSupportsCancellation = true;
            this.worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            this.worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            this.worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        }

        #region IAsyncWorker Members

        /// <summary>
        /// 
        /// </summary>
        public virtual event EventHandler<EventArgs> StatusChanged;

        /// <summary>
        /// 
        /// </summary>
        public void Start()
        {
            worker.RunWorkerAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        public void Stop()
        {
            worker.CancelAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        public bool CacellationPending
        {
            get { return worker.CancellationPending; }
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return worker.IsBusy; }
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            OnStatusChanged(sender, e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            OnStatusChanged(sender, e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            OnStatusChanged(sender, e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnStatusChanged(object sender, EventArgs e)
        {
            if (StatusChanged == null) return;
            StatusChanged(sender, e);
        }
    }
}